<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageMapper}.${table.nameClass}Mapper">
	
	<!--主表表名 -->
	<sql id="tableName">
		${table.nameTable}
	</sql>
	
	<!-- 常规查询字段 -->
	<sql id="fieldh">
<#list table.listColumn as column>
	<#if column_has_next>
		<#if column.nameCol == "deleted">
		case when deleted=1 then '是' else '否' end as deletedtext,
		<#else>
		${column.nameCol?uncap_first},
		</#if>
	<#else>
		<#if column.nameCol == "deleted">
		case when deleted=1 then '是' else '否' end as deletedtext
		<#else>
		${column.nameCol?uncap_first}
		</#if>
	</#if>
</#list>
	</sql>
	
	<!-- 插入字段 -->
	<sql id="fieldInsert">
<#list table.listColumn as column>
	<#if column_has_next>
		${column.nameCol?uncap_first},
	<#else>
		${column.nameCol?uncap_first}
	</#if>
</#list>
	</sql>
	
	<!-- 插入字段值 -->
	<sql id="fieldValue">
<#list table.listColumn as column>
	<#if column_has_next>
		${r'#{'}${column.nameCol?uncap_first}${r'}'},
	<#else>
		${r'#{'}${column.nameCol?uncap_first}${r'}'}
	</#if>
</#list>
	</sql>
	
	<sql id="query_where">       
		<if test="wherestr != null and wherestr != ''">
			<!-- 常规查询条件字符串 -->
			and ${r'${'}wherestr${r'}'}
		</if>      
		<if test="datascode != null and datascode != ''">
			<!-- 数据权限查询条件字符串 -->
			and ${r'${'}datascode${r'}'}
		</if> 
    </sql>

	<sql id="select_main">
		<!-- 主查询语句主体 -->
		<include refid="fieldh"></include>
		from <include refid="tableName"></include> t1
	</sql>
	
	<!-- 查询 -->
	<select id="select" parameterType="java.util.Map" resultType="${table.nameClass}">
		select 
		<include refid="select_main"></include>
		<where>
			<include refid="query_where"/>
		</where>
	</select>
	
	<!-- 查询指定单nid的首记录 -->
	<select id="queryFirstByGuid" parameterType="String" resultType="${table.nameClass}">
		select top 1 
		<include refid="select_main"></include>
		<where>
			guid = ${r'#{'}guid${r'}'}
		</where>
	</select>


	<!-- 查询实体 -->
	<select id="find${table.nameClass}" parameterType="java.util.Map" resultType="${table.nameClass}">
		select 
		<include refid="fieldh"></include>
		from 
		<include refid="tableName"></include> t1
		<where>
			<include refid="query_where"/>
		</where>
	</select>

	<!-- 新增实体 -->
	<insert id="insert${table.nameClass}" parameterType="${table.nameClass}">
        insert into 
        <include refid="tableName"></include> (
        <include refid="fieldInsert"></include>
        ) values (
        <include refid="fieldValue"></include>
        )
    </insert>
    
    <!-- 通过guid修改实体 -->
    <update id="updateByGuid" parameterType="${table.nameClass}">
    	update 
    	<include refid="tableName"></include>
		set
<#list table.listColumn as column>
	<#if column_has_next>
			${column.nameCol} = ${r'#{'}${column.nameCol?uncap_first}${r'}'},
	<#else>
			${column.nameCol} = ${r'#{'}${column.nameCol?uncap_first}${r'}'}
	</#if>
</#list>
    	where Guid = ${r'#{'}guid${r'}'}
    </update>
</mapper>